package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    //查询所有权限信息
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    //新增权限数据
    @Override
    public void add(Permission permission) {
        //创建日期
        permission.setCreateDate(new Date());
        permissionDao.add(permission);
    }

    //分页查询权限数据
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //使用基于mybatis框架提供的PageHelper进行分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.selsectByCondition(queryString);
        //将Page对象转换为PageResult对象
        PageResult result = new PageResult(page.getTotal(), page.getResult());

        return result;
    }

    //删除权限数据cff
    @Override
    public Result deleteById(Integer id) {
        try {
            permissionDao.delete(id);
            return new Result(true,"权限删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"该权限有角色关联，不能删除!");
        }
    }

    //编辑权限数据
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    //通过ID查找权限数据
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }
}
